//
//  WheelImageViewHelper.m
//  CheHui
//
//  Created by lanou3g on 16/3/2.
//  Copyright © 2016年 王长召. All rights reserved.
//

#import "WheelImageViewHelper.h"
#import <UIImageView+WebCache.h>

@interface WheelImageViewHelper()

@property (nonatomic,strong) UIScrollView *scrollerView;
@property (nonatomic,strong) UIPageControl *pageControl;
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,assign) CGFloat Duration; //  动画时间
@property (nonatomic,assign) CGFloat timeInterval;  //  时间间隔

@end

@implementation WheelImageViewHelper

- (instancetype)initWithFrame:(CGRect)frame array:(NSArray *)array timeInterval:(CGFloat)timeInterval animateDuration:(CGFloat)animateDuration
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setViewsWithArray:(NSArray *)array];
        self.Duration = animateDuration;
        self.timeInterval = timeInterval;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(timeAction) userInfo:nil repeats:YES]; 
        
    }
    return self;
}
- (void)timeAction {
    if (self.scrollerView.contentOffset.x / self.frame.size.width == self.scrollerView.subviews.count-3) {
        [self.scrollerView removeObserver:self forKeyPath:@"contentOffset" context:@"pianyi"];
        [UIView animateWithDuration:self.Duration animations:^{
            self.scrollerView.contentOffset = CGPointMake(self.scrollerView.contentOffset.x + self.scrollerView.frame.size.width, 0);
        }];
        self.scrollerView.contentOffset = CGPointMake(self.frame.size.width, 0);
        self.pageControl.currentPage = 0;
        [self.scrollerView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"pianyi"];
    }else {
        
            [UIView animateWithDuration:self.Duration animations:^{
                self.scrollerView.contentOffset = CGPointMake(self.scrollerView.contentOffset.x + self.scrollerView.frame.size.width, 0);
            }];
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    if ([keyPath  isEqualToString: @"contentOffset"]) {
        if ((int)self.scrollerView.contentOffset.x % (int)self.frame.size.width == 0) {
            if ((int)self.scrollerView.contentOffset.x == self.scrollerView.contentSize.width - self.scrollerView.bounds.size.width) {

                self.scrollerView.contentOffset = CGPointMake(self.frame.size.width, 0);
                self.pageControl.currentPage = 0;

            } else {
                self.pageControl.currentPage = self.scrollerView.contentOffset.x / self.frame.size.width - 1;
            }
            if ((int)self.scrollerView.contentOffset.x == 0) {

                self.scrollerView.contentOffset = CGPointMake(self.scrollerView.contentSize.width - self.scrollerView.bounds.size.width * 2, 0);
                self.pageControl.currentPage = self.scrollerView.contentSize.width / self.scrollerView.frame.size.width - 2;

            } else {
                self.pageControl.currentPage = self.scrollerView.contentOffset.x / self.frame.size.width - 1;
            }
        }
    }
}


- (void)setViewsWithArray:(NSArray *)array {
    self.scrollerView = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.scrollerView.contentSize = CGSizeMake((array.count + 2) * self.frame.size.width, 0);
    self.scrollerView.pagingEnabled = YES;
    self.scrollerView.bounces = NO;
    self.scrollerView.showsHorizontalScrollIndicator = NO;
    self.scrollerView.contentOffset = CGPointMake(self.frame.size.width, 0);
    [self.scrollerView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"pianyi"];
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0 ,self.frame.size.width ,self.frame.size.height)];
    imageV.userInteractionEnabled = YES;
    [imageV sd_setImageWithURL:array.lastObject];
    [self.scrollerView addSubview:imageV];
    for (int i = 0; i < array.count + 1; i++) {
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake((i+1) * self.frame.size.width, 0 ,self.frame.size.width ,self.frame.size.height)];
        imageV.userInteractionEnabled = YES;
        if (i != array.count) {
            [imageV sd_setImageWithURL:array[i]];
            [self.scrollerView addSubview:imageV];
        } else {
            [imageV sd_setImageWithURL:array.firstObject];
            [self.scrollerView addSubview:imageV];
        }
        
    }
    [self addSubview:self.scrollerView];
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 30, self.frame.size.width, 30)];
    self.pageControl.enabled = NO;
    self.pageControl.numberOfPages = array.count;
    self.pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    [self addSubview:self.pageControl];
}









/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
